import { postRequest, getRequest } from "request";
import { AliApplication } from "../../../const";
import { API_Ali_Auth, API_Ali_User_Info } from "../../api";
import type { BaseErr } from "utils";

// 解析code
async function _codeParse(code: string): Promise<[BaseErr?, any?]> {
    return await postRequest(API_Ali_Auth)({
        client_id: AliApplication.appId,
        client_secret: AliApplication.secret,
        grant_type: "authorization_code",
        code,
    });
}

// 获取用户信息
async function _getUserInfo(token: string) {
    return await getRequest(API_Ali_User_Info)(
        {},
        {
            contentType: "json",
            headers: {
                Authorization: token,
            },
        }
    );
}

export { _codeParse, _getUserInfo };
